package com.zscat.mallplus.center;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.util.WebUtils;

import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;

public class BaseController {

    @Autowired
    private IUmsMemberService memberService;
	
	/**
	 * 通用请求头
	 * 
	 * @param response
	 * @param request
	 */
	protected void setOriginHeader(HttpServletResponse response, HttpServletRequest request) {
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("origin"));
		response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
		response.setHeader("Access-Control-Allow-Credentials", "true");
	}
	
	/**
	 * 获取域名
	 * 
	 * @param request
	 * @return
	 */
	protected String getBasePath(HttpServletRequest request) {
		String basePath = (String) WebUtils.getSessionAttribute(request, "basePath");
		if (basePath == null) {
			basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
					+ request.getContextPath() + "/";
			WebUtils.setSessionAttribute(request, "basePath", basePath);
		}
		return basePath;
	}
	
	/**
	 * 响应返回结果
	 * 
	 * @param rows 影响行数
	 * @return 操作结果
	 */
	protected CommonResult toAjax(int rows) {
		return rows > 0 ? new CommonResult().success() : new CommonResult().failed();
	}
	
	/**
	 * 响应返回结果
	 * 
	 * @param rows 影响行数
	 * @return 操作结果
	 */
	protected CommonResult toAjax(Boolean rows) {
		return rows ? new CommonResult().success() : new CommonResult().failed();
	}

	/**
	 * 获取当前用户信息
	 * @return
	 */
	protected UmsMember getUser() {
		return memberService.getNewCurrentMember();
	}
	
}
